﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace Asteroids
{
    class AlienShip : GameObject
    {
        Texture2D sprite;
        SpriteBatch spriteBatch;
        public bool IsAlive {get; set;}
        SoundEffect soundEffectDead;

        public AlienShip(Game g, Vector2 position)
            : base(g)
        {
            Position = position;
            Random rand = new Random();
            Velocity = new Vector2(rand.Next(-140, 140), rand.Next(-140, 140));
            IsAlive = true;
            collided();
        }

        protected override void LoadContent()
        {
            sprite = Game.Content.Load<Texture2D>("AlienShipN");
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            soundEffectDead = Game.Content.Load<SoundEffect>("alien_Explosion");
        }

        public override void Draw(GameTime gameTime)
        {
            float scale = 1.0f;

            Vector2 offset =
             new Vector2((float)sprite.Width / 2f,
                         (float)sprite.Height / 2f);

            float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;

            Position += Velocity * elapsedSeconds;                                  // Updates alien's position

            wrapAround(800, 500, sprite.Width / 2f * scale, sprite.Height / 2f * scale);     //Wraps around if obj goes off-screen

            boundingRectangle = new Rectangle((int)Position.X - (int)offset.X,      //Updates obj's boundingRectangle
                                              (int)Position.Y - (int)offset.Y,
                                              (int)(sprite.Width * scale),
                                              (int)(sprite.Height * scale));

            spriteBatch.Begin();
            spriteBatch.Draw(sprite,
                             Position,
                             null,
                             Color.White,
                             Rotation,
                             offset,
                             scale,
                             SpriteEffects.None,
                             0);
            spriteBatch.End();
        }

        public void Dies()
        {
            IsAlive = false;
            soundEffectDead.Play();
        }
    }
}
